package com.lemon.cloud.datascope.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.lemon.cloud.admin.feign.DataScopeFeign;
import com.lemon.cloud.comm.model.RetOps;
import com.lemon.cloud.comm.po.RoleApplication;
import com.lemon.cloud.comm.po.SysOrg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DataScopeService {

    @Autowired
    private DataScopeFeign feign;


    public List<RoleApplication> getDSList(List<String> roleCodeList, String appId) {
        if (CollectionUtil.isEmpty(roleCodeList)) {
            return Lists.newArrayList();
        }
        List<RoleApplication> data = RetOps.of(feign.getDSList(roleCodeList)).getData().orElse(Lists.newArrayList());
        return data.stream().filter(item->appId.equals(item.getAppId().toString())).collect(Collectors.toList());
    }

    public List<Long> getDescendantList(List<Long> orgs){
        List<SysOrg> sysOrgs = RetOps.of(feign.getDescendantList(StrUtil.join(StrUtil.COMMA, orgs))).getData()
                .orElse(Lists.newArrayList());
        return sysOrgs.stream().map(SysOrg::getId).collect(Collectors.toList());
    }
}
